package hotel.action;

import hotel.service.IBedTypeService;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import common.action.BaseAction;
import common.pojo.BedType;
import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;
import static common.utils.Constants.*;

@Scope("prototype")
@Controller("bedTypeAction")
public class BedTypeAction extends BaseAction {

	/**
	 * 获取组
	 * 
	 * @return
	 */
	public void getBedTypeLists() throws Exception {
		bedTypeList = bedTypeService.find();
		JSONArray array = JSONArray.fromObject(bedTypeList);
		response.getWriter().write("{\"rows\":" + array.toString() + "}");
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getBedTypeListByPage() throws Exception {

		String currentTmp = request.getParameter("current");
		int total = bedTypeService.getCount();
		int current = PageUtil.getCurrent(currentTmp);
		Page page = PageUtil.createPage(LIMIT, total, current);
		bedTypeList = bedTypeService.find(page.getBeginIndex(), LIMIT, "id",
				"desc");
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"hotelBrand!getBedTypeListByPage.action");
		return "bedTypeListByPage";
	}


	/**
	 * 新建
	 * 
	 * @return
	 */
	public String save() {
		bedTypeService.save(bedType);
		if (StringUtils.isBlank(dofunction))dofunction = "()";// 刷新页面
		return "success2";
	}

	/**
	 * 排序
	 * 
	 * @return
	 * @throws IOException
	 */

	public String sort() throws IOException {
		String[] ids = request.getParameterValues("id");
		String[] sorts = request.getParameterValues("sort");
		bedTypeService.update(ids, sorts);
		return SUCCESS;

	}

	/**
	 * 根据id查找信息
	 * 
	 * @return
	 */
	public String findInfoById() {
		int id = Integer.valueOf(request.getParameter("id"));
		bedType = bedTypeService.findInfoById(id);
		return "findSuccess";
	}

	/**
	 * 更新
	 * 
	 * @return
	 */
	public String update() {
		bedTypeService.update(bedType);
		if (StringUtils.isBlank(dofunction))dofunction = "()";// 刷新页面
		return "success2";
	}

	public String deleteByIds() {
		String ids = request.getParameter("ids");
		bedTypeService.delete(ids);
		return SUCCESS;

	}

	private static final long serialVersionUID = 1L;

	@Autowired
	private IBedTypeService bedTypeService;
	private List<BedType> bedTypeList;
	private String pagination;
	private BedType bedType;
	private String dofunction;

	public List<BedType> getBedTypeList() {
		return bedTypeList;
	}

	public void setBedTypeList(List<BedType> bedTypeList) {
		this.bedTypeList = bedTypeList;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	public BedType getBedType() {
		return bedType;
	}

	public void setBedType(BedType bedType) {
		this.bedType = bedType;
	}

	public String getDofunction() {
		return dofunction;
	}

	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}

}
